
export const aesKey = (key) => {
  const length = key.length;
  if (length < 32) {
    for (let i = 0, len = 32 - length; i < len; i++) {
      key += "0";
    }
    return key;
  } else {
    return key.substring(0, 32);
  }
}
// AES加解密算法
export const aesEncrypt = (source, key)=>{
  key = window.CryptoJS.enc.Utf8.parse(aesKey(key)); //32位
  let iv = window.CryptoJS.enc.Utf8.parse("1234567890000000"); //16位
  //let srcs = window.CryptoJS.enc.Utf8.parse(source)
  let encrypted = window.CryptoJS.AES.encrypt(source, key, {
    iv: iv,
    mode: window.CryptoJS.mode.CBC,
    padding: window.CryptoJS.pad.Pkcs7,
  });
  return encrypted.toString();
}


export const aesDecrypt = (source, key)=>{
  key = window.CryptoJS.enc.Utf8.parse(aesKey(key)); //32位
  let iv = window.CryptoJS.enc.Utf8.parse("1234567890000000"); //16位
  //let srcs = window.CryptoJS.enc.Utf8.parse(source)
  let decrypted = window.CryptoJS.AES.decrypt(source, key, {
    iv: iv,
    mode: window.CryptoJS.mode.CBC,
    padding: window.CryptoJS.pad.Pkcs7,
  });
  return decrypted.toString(window.CryptoJS.enc.Utf8);
}